<template>
  <div class="Login">
    <div class="_2EfetPMmU0imM7hLRFJzr1" id="sidebarBox"></div>
    <div class="login__StyledCon-gfrfo0-0 iWgeis">
      <!-- <img src="https://openplantform.oss-cn-beijing.aliyuncs.com/account/login_background.png?OSSAccessKeyId=LTAI4FvBYgUM9wyFK4wa9nvy&amp;Expires=5178643850&amp;Signature=R%2BxRJ8evNFtOwy6Ni3Pi67paUGo%3D" alt="background" class="background-img" /> -->
      <div class="center-box">
        <div class="center-box-logo-img">
          <div class="logo">
            <!-- <span style="color:black;font-size: 1.3rem;">城乡房屋安全</span> -->
            <img :src="logo" alt="" mode="aspectFill" />
            <p>粤政易</p>
          </div>
        </div>
        <div class="center-box-top">
          <div class="center-box-msg"></div>
        </div>
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="ele-ruleForm" autocomplete="off" @keyup.enter.native="submitForm('ruleForm')">
          <el-form-item prop="username">
            <el-input type="text" v-model="ruleForm.username" autocomplete="new-password" class="el-input-pass" placeholder="请输入账号"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input :type="isSee ? 'text' : 'password'" v-model="ruleForm.password" :password="!isSee" autocomplete="new-password" placeholder="请输入密码"></el-input>
            <div class="passwordImg">
              <img v-if="isSee" src="../../../static/icon/ic_logon_display.png" mode="aspectFit" @click="isSee = false" />
              <img v-else-if="!isSee" src="../../../static/icon/ic_logon_hide.png" mode="aspectFit" @click="isSee = true" />
            </div>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="background-image: linear-gradient(90deg, #007aff 0%, #007aaa 100%)" @click="submitForm('ruleForm')">登录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
import { Login, getUserInfoGSa, getuserRelManyBiz, farmerHouseGetToken, MenuByUserSystemid, getLicenseExpiredDate, getDefaultSystem, checkUserOnline } from '../../utils/api'
import { isLinAn } from '@/utils/isLinAn'
export default {
  name: 'Login',
  data() {
    return {
      logining: false,
      isSee: false,
      logo: 'https://nfzjszlb.jst.zj.gov.cn/serverfile/zheliban/icon/ZLJ/9.png',
      userId: '',
      loginStyle: '',
      systemname: '',
      DanDianName: {},
      UUID: '',
      ruleForm: {
        username: '',
        password: ''
      },
      //杭州坐标 默认杭州 防止埋点失败
      buryPointParams: ['120.15', '30.28', 1, '05d46fe0a0d84719bc0f82024b0c88cd', '浙里办账号登录'],
      rules: {
        username: [{ required: true, message: '请输入账号', trigger: 'blur' }]
        // password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
      checked: true
    }
  },
  onLoad(option) {
    // this.isLoad();
  },
  //   onShow() {
  //         let countpage = uni.getStorageSync('countpage');
  //         if (countpage) {
  //             uni.removeStorageSync('countpage');
  //             ZWJSBridge.close();
  //         } else {
  //         }
  //     },
  mounted() {
    //进入组件
    // this.isLoad();
    // window.localStorage.clear()
    // window.localStorage.clear()
    // window.sessionStorage.clear()
    this.delCookie()
  },
  watch: {},
  methods: {
    //初始化加载
    //清空cookie
    delCookie() {
      var cookies = document.cookie.split(';')
      for (var i = 0; i < cookies.length; i++) {
        var cookie = cookies[i]
        var eqPos = cookie.indexOf('=')
        var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie
        document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/'
      }
      if (cookies.length > 0) {
        for (var i = 0; i < cookies.length; i++) {
          var cookie = cookies[i]
          var eqPos = cookie.indexOf('=')
          var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie
          var domain = location.host.substr(location.host.indexOf('.'))
          document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; domain=' + domain
        }
      }
    },
    setPoints1(data, data1) {
      const zwlog = new ZwLog({
        _user_id: data.userid,
        _user_nick: data.username
      })
      aplus_queue.push({
        action: 'aplus.setMetaInfo',
        arguments: ['_hold', 'BLOCK']
      })

      aplus_queue.push({
        action: 'aplus.setMetaInfo',
        arguments: ['_user_nick', data.username] //这个是用户名称的埋点
      })

      aplus_queue.push({
        action: 'aplus.setMetaInfo',
        arguments: ['_user_id', data.userid] //这个是用户Id的埋点
      })

      aplus_queue.push({
        action: 'aplus.setMetaInfo',
        arguments: ['_hold', 'START']
      })
      zwlog.onReady(function () {
        zwlog.sendPV({
          miniAppId: '2001905684',
          Page_duration: '10s',
          t2: '1.2s',
          t0: '0.2s',
          miniAppName: '粤政易',
          log_status: window.localStorage.getItem('token') ? '02' : '01'
        })
        // zwlog.record('yourTrackerEventCode', 'CLK', { Test1: '测试参数 1' })
        let { metaInfo } = zwlog
      })
    },
    //设置cookie
    setCookie(cName, value, expiredays) {
      var exdate = new Date()
      exdate.setDate(exdate.getDate() + expiredays)
      document.cookie = cName + '=' + value + (expiredays == null ? '' : ';expires=' + exdate.toGMTString())
    },
    getUIStyle() {
      ZWJSBridge.onReady(() => {
        ZWJSBridge.getUiStyle()
          .then((result) => {
            const { uiStyle } = result
            this.$store.commit('SetUIStyle', uiStyle)
          })
          .catch((error) => {
            this.$store.commit('SetUIStyle', 'normal')
          })
      })
    },
    isLoad: async function () {
      let _this = this
      //   this.getUIStyle();
      let ticket = this.getUrlParam('ticket')
      this.submitForm()
      if (ticket) {
        //拿到ticket后调用单点登录跳转首页 把信息带进去
        // this.submitForm()
        // this.$router.push({
        //   path: '/ZLJIndex',
        // });
        // const res = await getUserName(ticket);
        // if (res.data.code == 200) {
        //   this.DanDianName = res.data.data.dtalkUserInfo
        //   //将信息存在本地存储中
        //   window.localStorage.setItem('DanDianName', this.DanDianName)
        //   //获取当前设备的标识
        //   ZWJSBridge.getUUID({}).then(res => {
        //     this.UUID = res.uuid
        //   }).catch(err => {
        //   })
        //   //用户信息埋点
        //   this.setPoints(this.DanDianName, this.UUID)
        //   this.$router.push(
        //     {
        //       path: '/ZLJIndex',
        //     }
        //   )
        // } else {
        // }
      } else {
        const sUserAgent = window.navigator.userAgent.toLowerCase()
        const bIsDtDreamApp = sUserAgent.indexOf('dtdreamweb') > -1 // 浙里办APP
        const bIsAlipayMini = sUserAgent.indexOf('miniprogram') > -1 && sUserAgent.indexOf('alipay') > -1
        if (bIsDtDreamApp) {
          //   window.location.href =
          //     'https://puser.zjzwfw.gov.cn/sso/mobile.do?action=oauth&scope=1&servicecode=17ce0a9ca3034da28dda1cb381ab3d39&goto=https://mapi.zjzwfw.gov.cn/web/mgop/gov-open/zj/2001972466/lastTest/index.html';
        } else {
          //   window.location.href =
          //     'https://puser.zjzwfw.gov.cn/sso/mobile.do?action=oauth&scope=1&servicecode=17ce0a9ca3034da28dda1cb381ab3d39&goto=https://mapi.zjzwfw.gov.cn/web/mgop/gov-open/zj/2001972466/lastTest/index.html';
        }
      }
    },
    // 埋点
    setPoints(data, data1) {
      // 如采集用户信息是异步行为需要先执行这个BLOCK埋点
      const zwlog = new ZwLog({
          _user_id: data.userid,
          _user_nick: data.username,
        })
        zwlog.onReady(function () {
          zwlog.sendPV({
            miniAppId: '2001905684',
            Page_duration: '10s',
            t2: '1.2s',
            t0: '0.2s',
            miniAppName: '粤政易',
            log_status: window.localStorage.getItem('token') ? '02' : '01',
          })
          // zwlog.record('yourTrackerEventCode', 'CLK', { Test1: '测试参数 1' })
          let { metaInfo } = zwlog
        })
    },
    //得到网址参数
    getUrlParam(name) {
      var results = new RegExp('[?&]' + name + '=([^&#]*)').exec(window.location.href)
      if (results == null) {
        return null
      }
      return decodeURI(results[1]) || 0
    },
    submitForm: async function (ev) {
      let me = this
      var MD5 = require('../../utils/md5')
      var code = 'idl+=adfi@asdi'
      var pwd = MD5.b64_md5(MD5.b64_md5(this.ruleForm.password) + code.toUpperCase())
      const res = await Login(this.ruleForm.username, pwd)
      chalk.log('1234545565747564769999', chalk)
      if (res.data.code == 200) {
        //发起请求获取token
        const getUserData = await getUserInfoGSa(res.data.data)
        if (getUserData.data.code == 200) {
          // if(getUserData.data.data.deptId){
          //   let code1 = getUserData.data.data.deptId.slice(0, 4) + '00000000'
          //   window.localStorage.setItem('citycode', code1)
          // }
          if (getUserData.data.data.deptId !== null) {
            const res = await isLinAn(getUserData.data.data.deptId)
            if (res) {
              window.localStorage.setItem('citycode', '330112000000')
            } else {
              let code1 = getUserData.data.data.deptId.slice(0, 4) + '00000000'
              window.localStorage.setItem('citycode', code1)
            }
          } else {
            window.localStorage.setItem('citycode', '441300000000')
          }

          //是否注册
          window.localStorage.setItem('ISZHUCE', getUserData.data.data.userId)
          window.localStorage.setItem('userId', getUserData.data.data.bizUserId)
          window.localStorage.setItem('userName', getUserData.data.data.username)
          if (getUserData.data.data.username && getUserData.data.data.username.length > 12) {
            window.localStorage.setItem('isPer', 2)
          } else {
            window.localStorage.setItem('isPer', 1)
          }
          //将信息存在本地存储中
          window.localStorage.setItem('DanDianName', getUserData.data.data.username)
          window.localStorage.setItem('DanDianPhone', getUserData.data.data.username)
          window.localStorage.setItem('DanDianid', '360781199807156133')
        }
        // if (getToken.data.access_token) {
        // this.judgeUserCitys()
        this.$router.push({
          path: '/ZLJIndex',
        })
        window.localStorage.setItem('token', res.data.data)
        this.setPoints(res.data.data)
        // window.localStorage.setItem('userId', getToken.data.userInfo.userId)
        // window.localStorage.setItem('userName', JSON.stringify(getToken.data.userInfo))
        // window.localStorage.setItem('role', JSON.stringify(getToken.data.userInfo.roles))
        //   this.$message.success('登录成功！');
        // if (getToken.data.userInfo.roles == '{"8":"工匠/建筑公司"}' || getToken.data.userInfo.roles == '{"8":"工匠"}') {
        //   this.$store.commit('getIsGJ', false)
        // } else {
        //   this.$store.commit('getIsGJ', true)
        // }
        // this.$toast.success('登录成功！');
        // this.$notify({ type: 'success', message: '登录成功！' });
      } else {
        this.$message.error('登录失败！')
      }
    },
    //判断当前用户是不是多地市
    judgeUserCitys: async function () {
      try {
        const res = await getuserRelManyBiz(window.localStorage.getItem('username'))
        if (res.data.data.length > 0) {
          window.sessionStorage.setItem('cityDuo', res.data.data)
        } else {
          // window.sessionStorage.setItem('cityDuo',[])
        }
        if (res.data.code == 200 && !res.data.data) {
          this.$router.push({
            path: '/ZLJIndex'
          })
        } else if (res.data.code == 200 && res.data.data) {
          this.$router.push({
            path: '/citySelect'
          })
        }
      } catch (err) {
        this.$router.push({
          path: '/ZLJIndex'
        })
      }
    }
  }
}
</script>

<style scoped lang="scss">
.Login {
  max-width: 100vw;
  margin: 0px auto;
  min-width: 80vw;
  height: 100vh;
}

.passwordImg {
  position: absolute;
  right: 10px;
  top: 0px;
  img {
    width: 18px;
    height: 12px;
  }
}

.logo {
  text-align: center;

  image {
    margin: 0 auto;
    width: 90px;
    height: 90px;
    border-radius: 50%;
  }
}

._2EfetPMmU0imM7hLRFJzr1 {
  position: fixed;
  right: 0;
  top: 40%;
  z-index: 10;
  box-shadow: 0px 1px 11px 0px rgba(0, 0, 0, 0.15);
}

.iWgeis {
  position: fixed;
  top: 0px;
  left: 0px;
  width: 100%;
  height: 100%;
  background: white;
  overflow: scroll;
}

.iWgeis .background-img {
  width: 100%;
  position: absolute;
  bottom: 0px;
}

.iWgeis .center-box {
  width: 300px;
  position: absolute;
  margin: auto;
  left: 0px;
  right: 0px;
  top: 90px;
  padding: 20px 20px 30px;
  border-radius: 10px;
  /* box-shadow: rgb(225, 232, 248) 0px 0px 10px; */
  background: white;
}

.iWgeis .center-box .center-box-logo-img {
  text-align: center;
  font-size: 24px;
  font-weight: 500;
}

img {
  vertical-align: middle;
}

.iWgeis .center-box .center-box-logo-outer {
  width: 100%;
  height: 100%;
  cursor: pointer;
}

.iWgeis .center-box .center-box-top {
  margin: 0px auto;
  width: 100px;
}

.iWgeis .center-box .center-box-msg {
  height: 36px;
  line-height: 36px;
  color: rgb(208, 2, 27);
  text-align: center;
  font-size: 14px;
}

.ele-ruleForm {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  color: rgba(0, 0, 0, 0.65);
  font-size: 14px;
  font-variant: tabular-nums;
  line-height: 1.5;
  list-style: none;
  -webkit-font-feature-settings: 'tnum';
  font-feature-settings: 'tnum';
  width: 100%;
}
</style>

<style>
.Login .el-form-item__content {
  margin-left: 0 !important;
}

.Login .el-input__inner {
  border-top: none;
  border-right: none;
  border-left: none;
  border-image: initial;
  border-bottom: 1px solid rgb(239, 239, 239);
  border-radius: 0px;
}

.Login .el-button {
  margin-top: 26px;
  padding: 12px 114px;
}
</style>
